<?php
require 'includes/config.php';
require 'includes/auth.php';
require 'includes/sanitize.php';
$data = sanitize($_GET);
function createTable($url){
	$returnmsg="
		<table border='1'>
		<th><a href='".$url."&sortby=firstname'>First Name</a></th>
		<th><a href='".$url."&sortby=lastname'>Last name</a></th>
		<th><a href='".$url."&sortby=position'>Position</a></th>
		<th><a href='".$url."&sortby=team'>Team</a></th>
		<th><a href='".$url."&sortby=score'>score</a></th>
		<th><a href='".$url."&sortby=assist'>assist</a></th>
		<th><a href='".$url."&sortby=stats'>+/-</a></th>
		<th><a href='".$url."&sortby=pentalybox'>Penalty min</a></th>
		<th><a href='".$url."&sortby=price'>Price</a></th>
		<th><a href='".$url."&sortby=saving'>saving%</a></th>
		<th><a href='".$url."&sortby=nrofteams'>#teams</a></th>
		<td><a href='".$url."&sortby=totalscoreforteams'>total leage points</a></td>";
	return $returnmsg;
}
function insertPlayer($playerobj){
	$returnmsg.="<tr>
		<td><a href='viewplayers.php?playerid=".$playerobj->{'id'}."'>".$playerobj->{'firstname'}."</a></td>
		<td>".$playerobj->{'lastname'}."</td>
		<td>".$playerobj->{'position'}."</td>
		<td>".$playerobj->{'team'}."</td>
		<td>".$playerobj->{'score'}."</td>
		<td>".$playerobj->{'assist'}."</td>
		<td>".$playerobj->{'stats'}."</td>
		<td>".$playerobj->{'pentalybox'}."</td>
		<td>".$playerobj->{'price'}."</td>
		<td>".$playerobj->{'saving'}."</td>
		<td>".$playerobj->{'nrofteams'}."</td>
		<td>".$playerobj->{'totalscoreforteams'}."</td></tr>";
	return $returnmsg;
}

if(isset($data['playerid'])){
	// Display the specific player
	$request = new makeRequest($backend.'api.php?method=player&id='.$data['playerid'],'GET');
	$tmp = $request->makeRequest();
	$rbody = $tmp[0];
	$http_code = $tmp[1];
	$obj = json_decode($rbody);
	$playerid = $obj[0]->{'id'};
	/* Check for errors */
	if($http_code == 200){
		$msg.= createTable($url);
		$msg.= insertPlayer($obj[0]);
		$msg.="</table>";

		$msg2.= "Add player to team:";
		$request = new makeRequest($backend.'api.php?method=team&owner='.$_SESSION['uid'], 'GET');
		$tmp = $request->makeRequest();
		$rbody = $tmp[0];
		$obj = json_decode($rbody);
		$http_code = $tmp[1];
		// Check so there is no errors
		if($http_code == 200 && $obj[0]->{'id'} != ""){
			// Create dropdown list with all the leagues the current user owns
			$msg2.= '<form method="get" action="teams.php"><select name="teamid">';
			foreach($obj as &$value){
				$msg2.= "<option value=".$value->{'id'}.">".$value->{'name'}."</option>";
			}
			$msg2.= '</select>
			<input type="hidden" name="playerid" value="'.$playerid.'" />
			<input type="submit" Value="Submit" name="submit" /></form>'; 
		}
	}
}else{
	$tmp = '&firstname='.urlencode($data['firstname']).'&lastname='.urlencode($data['lastname']).
	'&position='.urlencode($data['position']).'&team='.urlencode($data['team']).'&order='
	.$data['order'].'&startat='.$data['startat'].'&shownr=30&sortby='.$data['sortby'];
	$request = new makeRequest($backend.'api.php?method=player'.$tmp,'GET');
	$tmp = $request->makeRequest();
	$rbody = $tmp[0];
	$http_code = $tmp[1];
	$obj = json_decode($rbody);
	/* Check for errors */
	if($http_code == 200){
		// Figure out if the user want to switch between asc/desc
		if($data['order'] == "ASC"){
			$order = "DESC";
		}else{
			$order = "ASC";
		}
		$url2="viewplayers.php?firstname=".$data['firstname']."&lastname=".$data['lastname']."&team=".$data['team'].
		"&position=".$data['position'];
		$url = $url2.="&order=".$order;
		$msg.= createTable($url);
		foreach($obj as &$value){
			$msg.= insertPlayer($value);
		}
		$msg.="</table>";
	}
}
require 'includes/header.php';
require 'includes/topmenu.php';
echo '<div id=kaka>';
require 'includes/sidemenu.php'
?>

<div id=content>
<form method="GET" action="<?php echo $PHP_SELF; ?>">Firstname:<input
	type="text" name="firstname" /> Lastname:<input type="text"
	name="lastname" /> <select name="position">
	<option id="ALLposs">All positions</option>
	<option id="CE">CE</option>
	<option id="LW">LW</option>
	<option id="RW">RW</option>
	<option id="LD">LD</option>
	<option id="RD">RD</option>
	<option id="GK">GK</option>
</select> <select name="team">
	<option id="ALLteams">All teams</option>
	<option id="LHF">LHF</option>
	<option id="AIK">AIK</option>
	<option id="VÄX">VÄX</option>
	<option id="MODO">MODO</option>
	<option id="SAIK">SAIK</option>
	<option id="HV71">HV71</option>
	<option id="TIK">TIK</option>
	<option id="DIF">DIF</option>
	<option id="BIF">BIF</option>
	<option id="FRÖ">FRÖ</option>
	<option id="LHC">LHC</option>
	<option id="FBK">FBK</option>
</select> <input type="submit" Value="submit" name="submit" /></form>


<?php

echo $msg;
echo "<br /><br />".$msg2;
if(!isset($data['playerid'])){
$startat = $data['startat']+30;
$stopat = 30;
echo "<a href='".$url2."&sortby=".$data['sortby']."&startat=".$startat.'&order='.$data['order']."'>Next page</a>";
}
?> <?php
require 'includes/footer.php';
?>